package org.me.SendSMS.adapter;

import java.util.List;

import org.me.SendSMS.MessageTypeActivity;
import org.me.SendSMS.R;
import org.me.SendSMS.data.Message;
import org.me.SendSMS.utli.GlobalVariable;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MessageAdapter  extends BaseAdapter {
	
	private LayoutInflater mInflater;
	private Context mContext;
	private List<Message> listMessage;
	
	
	public MessageAdapter(Context context, List<Message> list)
	{
		mContext 	= context;
		mInflater 	= LayoutInflater.from(context);
		listMessage = list;
       
	}
	
	public class ViewHolder
	{
		TextView txtTitle;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listMessage.size();
}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	
	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		// TODO Auto-generated method stub

		final ViewHolder holder;

		if (view == null)
		{
			holder 					= new ViewHolder();
			view 					= mInflater.inflate(R.layout.items_message, null);
			
			holder.txtTitle 		= (TextView)view.findViewById(R.id.txtTinNhan);
			
			view.setTag(holder);
		}
		
		else
		{
			holder = (ViewHolder) view.getTag();
		}

		
		holder.txtTitle.setTag(position);
		holder.txtTitle.setText(listMessage.get(position).GetTitle());
		holder.txtTitle.setOnLongClickListener(new OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				// TODO Auto-generated method stub
				IsSendSMSVietNamese(position);
				return false;
			}
		});
		holder.txtTitle.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext,
						"Press long click to select action !",
						Toast.LENGTH_LONG).show();	
			}
		});
		
		
		return view;
	}
	
	public void IsSendSMSVietNamese(final int position){
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		builder.setMessage("Select action ?")
		       .setCancelable(false)
		       .setPositiveButton("Tin nhắn có dấu", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		        	   GlobalVariable.isVietNamese = true;
		        	   Intent myIntent = new Intent(mContext, MessageTypeActivity.class);
						myIntent.putExtra("file",listMessage.get(position).GetID());
						mContext.startActivity(myIntent);
		        	   

		           }
		       })
		       .setNegativeButton("Tin nhắn không dấu", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		        	   GlobalVariable.isVietNamese = false;
		        	   Intent myIntent = new Intent(mContext, MessageTypeActivity.class);
						myIntent.putExtra("file",listMessage.get(position).GetID());
						mContext.startActivity(myIntent);
		           }
		       });
		AlertDialog alert = builder.create();			
		alert.show();
	}
}
